<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="导师编号">
              <a-input v-model="queryParam.dsbh"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="6" :sm="12">
              <a-form-item label="院系">
                <a-select allowClear showSearch :disabled="selectMenu.yx.length === 1"  :filterOption="filterOption" v-model="queryParam.dwh" @change="yxChange">
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <!-- <a-col :md="6" :sm="12">
              <a-form-item label="所属专业">
                <a-select allowClear showSearch :filterOption="filterOption" v-model="queryParam.zdzy">
                  <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
            <a-col :md="6" :sm="12">
              <a-form-item label="导师状态">
                <a-select allowClear v-model="queryParam.shzt">
                  <a-select-option v-for="i in TYPE0320" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-item label="是否在岗">
                <a-select allowClear v-model="queryParam.dssfzg">
                  <a-select-option v-for="i in TYPE0321" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="12" :sm="12">
              <a-form-item label="导师类别">
                <a-select mode="multiple" allowClear v-model="queryParam.dslbm">
                  <a-select-option v-for="i in TYPE0360" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="8" :sm="12">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
            <a-button type="primary" @click="exportExcel">导出</a-button>
            <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="{alert: false}"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
        :scroll="{x: 1600}"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <span slot="csrq" slot-scope="a">{{a && a.slice(0,11)}}</span>
        <a slot="dsbh" slot-scope="a, b" @click="direct(b)">{{a}}</a>
        <template slot="operation" slot-scope="a,b">
          <a @click="toEdit(b)">修改</a>
        </template>
      </s-table>
    </a-card>
  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import searchMixin from '@/utils/searchRemain'

export default {
  mixins: [ searchMixin ],
  data () {
    return {
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '导师编号',
          dataIndex: 'dsbh',
          scopedSlots: { customRender: 'dsbh' }
        },
        {
          title: '姓名',
          dataIndex: 'xm'
        },
        {
          title: '性别',
          dataIndex: 'xbmc'
        },
        {
          title: '身份证号',
          dataIndex: 'sfzjh'
        },
        {
          title: '出生日期',
          dataIndex: 'csrq',
          scopedSlots: { customRender: 'csrq' }
        },
        {
          title: '所属院系',
          dataIndex: 'ssyxmc'
        },
        {
          title: '指导专业',
          dataIndex: 'zdzymc',
          width:220
        },
        {
          title: '导师类别',
          dataIndex: 'dslbmc'
        },
        {
          title: '导师状态',
          dataIndex: 'shztmc'
        },
        {
          title: '是否在岗',
          dataIndex: 'dssfzgmc'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          width :'80px',
          fixed:'right'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.teacher.teacherList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {},
      recruitVisible: false,
      breakVisible: false,
      teacherInfo: {},
      initDone: false
    }
  },
  components: {STable},
  computed: {
    ...mapState({
      TYPE0360: state => state.app['TYPE0360'], // 导师类别
      TYPE0320: state => state.app['TYPE0320'], // 导师状态
      TYPE0321: state => state.app['TYPE0321'], // 是否在岗
      TYPE0387: state => state.app['TYPE0387'] // 招生学年
    })
  },
  // beforeRouteEnter (to, from, next) {
  //   next(vm => {
  //     let pageParams = vm.$store.state.app.pageParams || {}
  //     if ((from.name === '导师详细信息' || from.name === '导师信息补充页') && pageParams.path === vm.$route.path) {
  //       vm.queryParam = pageParams.params
  //       vm.parameter = pageParams.pagination
  //     }
  //     // vm.initDone = true
  //   })
  // },
  // beforeRouteLeave (to, from, next) {
  //   if (to.name === '导师详细信息' || to.name === '导师信息补充页') {
  //     this.$store.dispatch('app/setPageParams', {
  //       path: this.$route.path,
  //       params: this.queryParam,
  //       pagination: this.parameter
  //     })
  //   }
  //   next()
  // },
  created () {
    this.initSelectMenu()
    this.$store.dispatch('app/setDictionary', 'TYPE0360')
    this.$store.dispatch('app/setDictionary', 'TYPE0320')
    this.$store.dispatch('app/setDictionary', 'TYPE0321')
    this.$store.dispatch('app/setDictionary', 'TYPE0387')
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        if(this.selectMenu.yx && this.selectMenu.yx.length == 1){
          this.$set(this.queryParam, 'dwh', this.selectMenu.yx[0].dwh)
        }
        this.initDone = true
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$set(this.queryParam, 'zdzy', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    direct (data) {
      this.$router.push(`/teacher/info/${data.dsbh}`)
    },
    toEdit (data) {
      this.$router.push(`/teacher/info/${data.dsbh}/supplement`)
    },
    exportExcel () {
      let params = {...this.queryParam}
      delete params.pageNum
      this.$export('/teacher/dsxx/exportDsxxbc', params)
    }
  }
}
</script>

<style>

</style>
